package com.goldgov.mobile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.conference.service.Conference;
import com.goldgov.conference.service.ConferenceAttendance;
import com.goldgov.conference.service.ConferenceAttendanceService;
import com.goldgov.conference.service.ConferenceService;
import com.goldgov.conference.service.ConferenceUser;
import com.goldgov.conference.service.ConferenceUserService;
import com.goldgov.distancelearning.service.Distancelearning;
import com.goldgov.distancelearning.service.DistancelearningAttendance;
import com.goldgov.distancelearning.service.DistancelearningService;
import com.goldgov.distancelearning.service.DistancelearningUser;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.notice.service.ConferenceNoticeBean;
import com.goldgov.notice.service.Notice;
import com.goldgov.notice.service.NoticeBean;
import com.goldgov.notice.service.NoticeQuery;
import com.goldgov.notice.service.NoticeService;
import com.goldgov.notice.service.NoticeUser;
import com.goldgov.notice.service.NoticeUserQuery;
import com.goldgov.notice.service.NoticeUserService;
import com.goldgov.notifyrecord.service.NotifyRecord;
import com.goldgov.securityworkbench.dict.bean.dto.DictDto;
import com.goldgov.securityworkbench.dict.bean.dto.DictQuery;
import com.goldgov.utils.PrintUtil;
import com.goldgov.utils.PropertyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: NoticeForMobileController 
 * @Description: TODO
 * @author hanxiangyu
 * @date 2018年6月27日 下午5:11:40 
 */

@RestController
@RequestMapping("/mobile/notice")
@Api("通知公告移动接口")
public class NoticeForMobileController {
	

	@Autowired
	private NoticeService noticeService;
	@Autowired
	private NoticeUserService noticeUserService;
	@Autowired
	private ConferenceService conferenceService;
	//考勤记录服务接口
	@Autowired
	private ConferenceAttendanceService conferenceAttendanceService;
	//回执/会议记录与用户关系 服务接口
	@Autowired
	private ConferenceUserService conferenceUserService;
	
	@Autowired
	private DistancelearningService distancelearningService;
	
	
	

	@PostMapping("/studyWarning")
	@ApiOperation(value = "学习预警", notes = "学习预警" )
	public void studyWarning(NotifyRecord obj,HttpServletRequest request,Model model,HttpServletResponse response) {
		try {
			LoginAuthUser user=LoginAuthUser.getUser(request);
			obj.setSenderId(user.getEntityID());//发送人ID
			obj.setSenderName(user.getName());//发送人名称
			String[] userIds = new String[]{obj.getReceiverId()};
			obj.setUserIds(userIds);
			noticeService.addStudyWarning(obj);
			PrintUtil.printMsg(response, true,"通知成功");
		} catch (Exception e) {
			e.printStackTrace();
			PrintUtil.printMsg(response, false,"预警通知失败");
		}		
	}
	
	
	
	@GetMapping("/")
	@ApiOperation(value = "通知公告", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchType", value = "列表类型", paramType = "query"),
		@ApiImplicitParam(name = "searchPulishBeginDate", value = "发布时间", paramType = "query"),
		@ApiImplicitParam(name = "searchPulishEndDate", value = "发布时间", paramType = "query"),
		@ApiImplicitParam(name = "searchNoticeType", value = "消息类型", paramType = "query")})
	public JsonQueryObject<Notice> listNoticeFull(@ApiIgnore NoticeQuery query,HttpServletRequest request){
		//收件箱
		LoginAuthUser u=LoginAuthUser.getUser(request);
		query.setSearchReceiveUserID(u.getEntityID());
		List<Notice> list = noticeService.findMobileNoticeList(query);
		DictQuery queryDict=new DictQuery();
		queryDict.setQueryDictCode("d_noticeType");
		queryDict.setPageSize(-1);
		List<DictDto> listDict=noticeService.findDictListByPage(queryDict);
		for(Notice notice:list) {
			if (PropertyUtil.objectNotEmpty(notice.getNoticeType())) {
				for(DictDto d:listDict) {
					if(d.getDictKey().equals(notice.getNoticeType())) {
						if(PropertyUtil.objectNotEmpty(notice.getNoticeName())) {
							notice.setNoticeName("【"+d.getDictValue()+"】"+notice.getNoticeName());	
						}else {
							notice.setNoticeName(notice.getNoticeName());
						}
						break;
					}
				}
			}else {
				notice.setNoticeName("【通知】"+notice.getNoticeName());	
			}
		}
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/getUnreadNum")
	@ApiOperation(value = "获取未读数", notes ="")
	public JsonSuccessObject<Integer> getUnreadNum(HttpServletRequest request,HttpServletResponse response){
		LoginAuthUser u=LoginAuthUser.getUser(request);
		//获取未读数
		int unreadNum = noticeService.getUnreadNum(u.getEntityID());
		JsonSuccessObject<Integer> returnResult = new JsonSuccessObject<>(unreadNum);
		return returnResult;
	}
	@GetMapping("/getNoticeByID")
	@ApiOperation(value = "根据主键查询会议记录", notes ="工作日志详情")
	public JsonSuccessObject<Notice> getNotice( String id,HttpServletRequest request,HttpServletResponse response,Model model){
		LoginAuthUser u=LoginAuthUser.getUser(request);	
		//修改状态已读
		NoticeUser noticeUer=new NoticeUser();
		noticeUer.setNoticeID(id);
		noticeUer.setUserID(u.getEntityID());
		noticeUer.setIsRead(NoticeUserQuery.IS_READ_YES);
		noticeUserService.updateNoticeUser(noticeUer);
		NoticeBean notice = noticeService.findNoticeById(id,u.getEntityID());
		
		//如果回执状态为空查看详情时更新为我知道了
		if (!PropertyUtil.objectNotEmpty(notice.getReceiptState())) {
			NoticeUser nu = new NoticeUser();
			nu.setNoticeID(notice.getNoticeID());
			nu.setReceiptState(NoticeUserQuery.RECEIPT_STATE_LEAVE);
			nu.setReceiptContent("我知道了");
			this.updateMettingReceipt(nu, request, model, response);
		}
		if (PropertyUtil.objectNotEmpty(notice.getNoticeType())) {
			if ("1".equals(notice.getNoticeType())) {//三会一课
				if (PropertyUtil.objectNotEmpty(notice.getSourceID())) {
					notice.setConference(noticeService.getConferenceById(notice.getSourceID()));
				}
			}else if ("2".equals(notice.getNoticeType())) {//远程教育
				if (PropertyUtil.objectNotEmpty(notice.getSourceID())) {
					notice.setConference(noticeService.getDisById(notice.getSourceID()));
				}
			}else if ("4".equals(notice.getNoticeType())) {//三会一课和远程教育
				if (PropertyUtil.objectNotEmpty(notice.getSourceID())) {
					notice.setConference(noticeService.getConferenceById(notice.getSourceID()));
				}
			}
		}
		DictQuery query=new DictQuery();
		query.setQueryDictCode("d_noticeType");
		query.setPageSize(-1);
		List<DictDto> list=noticeService.findDictListByPage(query);
		if (PropertyUtil.objectNotEmpty(notice.getNoticeType())) {
			for(DictDto d:list) {
				if(d.getDictKey().equals(notice.getNoticeType())) {
					if(PropertyUtil.objectNotEmpty(notice.getNoticeName())) {
						notice.setNoticeName("【"+d.getDictValue()+"】"+notice.getNoticeName());	
					}else {
						notice.setNoticeName("【"+d.getDictValue()+"】");
					}
					break;
				}
			}
		}else {
			if(PropertyUtil.objectNotEmpty(notice.getNoticeName())) {
				notice.setNoticeName("【通知】"+notice.getNoticeName());	
			}else {
				notice.setNoticeName("【通知】");	
			}
		}
		JsonSuccessObject<Notice> returnResult = new JsonSuccessObject<>(notice);
		return returnResult;
	}
	
	
	@PostMapping("/updateReceipt")
	@ApiOperation(value = "回执操作", notes ="")
	public void activeConference(NoticeUser obj,HttpServletRequest request,Model model,HttpServletResponse response){
		LoginAuthUser u=LoginAuthUser.getUser(request);	
		obj.setUserID(u.getEntityID());
		if (noticeUserService.updateNoticeUser(obj)) {
			NoticeBean notice = noticeService.findNoticeById(obj.getNoticeID(),u.getEntityID());
			if (PropertyUtil.objectNotEmpty(notice) && PropertyUtil.objectNotEmpty(notice.getNoticeType())) {
				if ("1".equals(notice.getNoticeType())) {//三会一课
					if (PropertyUtil.objectNotEmpty(notice.getSourceID())) {
						ConferenceUser con = new ConferenceUser();
						con.setConferenceID(notice.getSourceID());
						con.setReceiptType(notice.getReceiptState());
						if (PropertyUtil.objectNotEmpty(notice.getReceiptContent())) {
							con.setReceiptInfomation(notice.getReceiptContent());
						}
						this.updateReceipt(con, request, response);
					}
				}else if ("2".equals(notice.getNoticeType())) {//远程教育
					if (PropertyUtil.objectNotEmpty(notice.getSourceID())) {
						DistancelearningUser disUser = new DistancelearningUser();
						disUser.setDistancelearningID(notice.getSourceID());
						disUser.setReceiptType(notice.getReceiptState());
						if (PropertyUtil.objectNotEmpty(notice.getReceiptContent())) {
							disUser.setReceiptInfomation(notice.getReceiptContent());
						}
						this.updateReceiptDis(disUser, request, response);
					}
				}else if ("4".equals(notice.getNoticeType())) {//三会一课和远程教育
					if (PropertyUtil.objectNotEmpty(notice.getSourceID())) {
						ConferenceUser con = new ConferenceUser();
						con.setConferenceID(notice.getSourceID());
						con.setReceiptType(notice.getReceiptState());
						if (PropertyUtil.objectNotEmpty(notice.getReceiptContent())) {
							con.setReceiptInfomation(notice.getReceiptContent());
						}
						this.updateReceipt(con, request, response);
						DistancelearningUser disUser = new DistancelearningUser();
						disUser.setDistancelearningID(notice.getSourceID());
						disUser.setReceiptType(notice.getReceiptState());
						if (PropertyUtil.objectNotEmpty(notice.getReceiptContent())) {
							disUser.setReceiptInfomation(notice.getReceiptContent());
						}
						this.updateReceiptDis(disUser, request, response);
					}
				}
			}
			if (obj.getReceiptState().intValue() == 3) {
				PrintUtil.printMsg(response, true, "请假成功!");
			} else {
				PrintUtil.printMsg(response, true, "操作成功!");
			}
		}else {
			PrintUtil.printMsg(response, false,"操作失败!");
		}
	}	
	
	
	
	//三会一课回执请假操作
	public void updateReceipt(ConferenceUser obj,HttpServletRequest request,HttpServletResponse response){
		//回执时间
		LoginAuthUser u=LoginAuthUser.getUser(request);
		if (obj.getUserID() == null || "".equals(obj.getUserID())) {
			obj.setUserID(u.getEntityID());
		}
		obj.setReceiptDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		conferenceUserService.updateReceipt(obj);
		ConferenceUser cu = conferenceUserService.getConferenceUser(obj);
		if (cu == null) {
			PrintUtil.printMsg(response, false,"您未参加此会议,不能进行签到!");
		}else {
			
			if (null != obj.getReceiptType() && obj.getReceiptType() == 3) {
				//通过关系表id确定是否存在考勤记录
				List<ConferenceAttendance> attendanceList = conferenceAttendanceService.getConferenceAttendanceList(cu.getConferenceUserID());
				if (!attendanceList.isEmpty()) {
					conferenceAttendanceService.deleteConferenceAttendance(cu.getConferenceUserID());
				}
			}
		}
	}
	//远程教育回执操作
	public void updateReceiptDis(DistancelearningUser obj,HttpServletRequest request,HttpServletResponse response){
		//回执时间
		obj.setReceiptDate(new Date());
		distancelearningService.disReceipt(obj);
		//确定是否存在考勤记录
		DistancelearningAttendance disAtt = new DistancelearningAttendance();
		disAtt.setUserID(obj.getUserID());
		disAtt.setDistancelearningID(obj.getDistancelearningID());
		List<DistancelearningAttendance> attendanceList = distancelearningService.getDisAttendance(disAtt);	
		if (!attendanceList.isEmpty()) {
			distancelearningService.deleteDisAttendance(disAtt);
		}
	}
	
	
	public void updateMettingReceipt(NoticeUser obj,HttpServletRequest request,Model model,HttpServletResponse response){
		LoginAuthUser u=LoginAuthUser.getUser(request);	
		obj.setUserID(u.getEntityID());
		if (noticeUserService.updateNoticeUser(obj)) {
			NoticeBean notice = noticeService.findNoticeById(obj.getNoticeID(),u.getEntityID());
			if (PropertyUtil.objectNotEmpty(notice) && PropertyUtil.objectNotEmpty(notice.getNoticeType())) {
				if ("1".equals(notice.getNoticeType())) {//三会一课
					if (PropertyUtil.objectNotEmpty(notice.getSourceID())) {
						ConferenceUser con = new ConferenceUser();
						con.setConferenceID(notice.getSourceID());
						con.setReceiptType(notice.getReceiptState());
						if (PropertyUtil.objectNotEmpty(notice.getReceiptContent())) {
							con.setReceiptInfomation(notice.getReceiptContent());
						}
						this.updateReceipt(con, request, response);
					}
				}else if ("2".equals(notice.getNoticeType())) {//远程教育
					if (PropertyUtil.objectNotEmpty(notice.getSourceID())) {
						DistancelearningUser disUser = new DistancelearningUser();
						disUser.setDistancelearningID(notice.getSourceID());
						disUser.setReceiptType(notice.getReceiptState());
						if (PropertyUtil.objectNotEmpty(notice.getReceiptContent())) {
							disUser.setReceiptInfomation(notice.getReceiptContent());
						}
						this.updateReceiptDis(disUser, request, response);
					}
				}else if ("4".equals(notice.getNoticeType())) {//三会一课和远程教育
					if (PropertyUtil.objectNotEmpty(notice.getSourceID())) {
						ConferenceUser con = new ConferenceUser();
						con.setConferenceID(notice.getSourceID());
						con.setReceiptType(notice.getReceiptState());
						if (PropertyUtil.objectNotEmpty(notice.getReceiptContent())) {
							con.setReceiptInfomation(notice.getReceiptContent());
						}
						this.updateReceipt(con, request, response);
						DistancelearningUser disUser = new DistancelearningUser();
						disUser.setDistancelearningID(notice.getSourceID());
						disUser.setReceiptType(notice.getReceiptState());
						if (PropertyUtil.objectNotEmpty(notice.getReceiptContent())) {
							disUser.setReceiptInfomation(notice.getReceiptContent());
						}
						this.updateReceiptDis(disUser, request, response);
					}
				}
			}
		}	
	}
	
}
